## 9.4. Graphics

Many people are fascinated by computer graphics. Computers are being used to create photo-realistic images of surreal scenes or fractally generated images of mountain ridges with lakes and valleys; to change images by bending, polishing, aging them; or to make any other manipulations.

Linux does not need to step shyly aside when it comes to graphics. It can do just about anything that other computing environments can do, and in some areas, like dealing with many graphics files at the same time, it even excels. The X Window System, described in the next chapter, forms a very good base for bitmapped graphics, while hardware support will eventually arrive for 3D graphics conforming to the OpenGL standard.

However, working with graphics on Linux is often different from what you might be used to from other operating systems; the Unix model of small, interoperating tools is in effect here, too. This philosophy is be illustrated most clearly with the suite of graphics manipulation programs `ImageMagick` that we will describe here. ImageMagick is a collection of tools that operate on graphics files and are started from the command line or from shell scripts. Imagine that you have two thousand files of one file format that you want to reduce to 256 colors, slant, and convert to another file format. On Linux, this is only a few lines in a shell script. Now imagine doing this on Windows: Click on the File menu, click on the Open menu entry, select a file, select an operation, specify the parameters for the operation in a dialog, click OK, choose save from the menu, select a filename, and then click OK. Now repeat for next 1999 files. Can you say RSI?[39]

> [39]Repetitive Strain Injury--a malady that comes from typing too much. The better-known carpal tunnel syndrome is one form of RSI.

Graphics can not only be drawn but also programmed. There are tools like the ray-tracer POVRAY presented in this chapter where you specify the graphics to be generated by a suite of commands, often in a full-blown graphics-programming language. While this is perhaps more difficult than drawing the desired graphics with the mouse, it is also infinitely more flexible once you have mastered how to use it.

Now, not everybody wants to work with graphics from the command line. Part of the appeal of working with computer graphics is the immediate feedback when working with graphics programs, and Linux can provide these, too, even though those programs are currently in the minority on Linux (and perhaps will continue to be).

In this section, we will talk about some of the options for using and working with graphics on your Linux system. Most of the programs discussed here require the X Window System, which we'll show you how to install and configure in later chapters.

The programs we are covering here are:

ImageMagick

  A number of command-line tools for image manipulation

GIMP

  A graphical image manipulation program

POVRAY

  A famous ray-tracer

There are many more graphics programs available for Linux, including 3D modelers and video players. Scouring some Linux archives will reveal a wealth of good software to you.

### 9.4.1. ImageMagick

ImageMagick is a suite of programs that convert and manipulate graphics files from the command line. It is also well suited for batch conversions, that is, converting many files at once. In addition, ImageMagick includes PerlMagick, a binding that lets you invoke the ImageMagick utilities from the Perl programming language. It comes with most Linux distributions; if yours does not have it, you can get it from ftp://ftp.x.org/contrib/applications/ImageMagick.

ImageMagick consists of a library and a number of utilities:

`display`

  `display` is a graphical frontend to many of the functions provided by ImageMagick. It is very useful if you want to experiment with the effects of a function before you go and batch-convert hundreds of images with these effects. `display` can load many different graphics file formats and display them on the X Window System. You can start `display` and pass it one or more filenames on the command line, or load a file from the menu provided. If you have started `display` and do not see a menu, click the image displayed with the left mouse button. Figure 9-4 shows `display` in action. `display` features a huge number of command-line switches. See the manual page for details.

  #### Figure 9-4. ImageMagick in action

`import`

  `import` is a small program to capture either one window or the whole server into a file; that is, it allows you to make screenshots. For example, to make a screenshot of your whole screen and save it as myscreen.xpm in the graphics file format XPM, you would execute:

  >     tigger$ import -window root myscreen.xpm

  When you hear the first beep, the screen capture begins, and when the second one sounds, capturing is finished and the image data is in the process of being saved to the file you specified.

  If you want to capture the contents of only one window, the easiest way is to start `import` without the `-window` option:

  >     tigger$ import mywindow.xpm

  The cursor then turns into a crossbar, which prompts you to click on any window. This window's contents are then captured and saved to the specified file.

  Like all ImageMagick programs, `import` has many command-line options; check the manual page.

`montage`

  `montage` is a very useful little tool with a functionality rarely found elsewhere. It takes a number of images and puts them together as one large image in a tiled manner. There are lots of options for changing how the images are exactly tiled and put together.

  In order to tile all your JPEG images in the current directory and create the image all.jpg out of it, you could call:

  >     tigger$ montage *.jpg all.jpg

  By default, there will be a label with the filename below each image. You can remove this by saying:

  >     tigger$ montage +frame *.jpg all.jpg

`convert`

  In a way, `convert` is the heart of the ImageMagick suite. It convert an amazing number of graphics formats. For example, let's assume that you want to port a GUI program from Windows to Linux. You have a large number of toolbar icons in Windows BMP format and want to convert those to XPM. This could be done with:

  >     for i in *.bmp
  >     do
  >     convert $i xpm:'basename $i .bmp'.xpm
  >     done

  `convert` will happily chug through all the images and convert them. If you want to do other things to the images, just add the necessary switches, e.g., `-despeckle` for removing speckles from the images.

`mogrify`

  `mogrify` is like `convert`, but it overwrites the original images and is meant more for applying filter functions than for converting file formats (even though you can easily change the file format by using the switch `-format`).

`identify`

  `identify` outputs information about the images passed on the command line. For example:

  >     tigger$ identify tux.gif
  >     tux.gif 257x303+0+0 DirectClass 10968b GIF 1s

  This tells you, among other things, that tux.gif is a GIF file 257 pixels wide and 303 pixels high. If this is not enough information for you, try the `-verbose` switch!

`combine`

  As its name indicates, `combine` combines several images into one. You can for example put color-separated images together or put a logo on top of a larger image.

`xtp`

  Like the `ftp` program, `xtp` downloads files from a remote site or uploads them to a remote site. But unlike `ftp`, it does not need interactive input (in order to get this with `ftp`, you would have to edit its configuration file). `xtp` can be very favorably combined with the other tools from ImageMagick suite to automatically manipulate images on remote servers.

To sum up, the programs from the ImageMagick suite are an extremely versatile means to manipulate graphics files. It takes some time to familiarize yourself with all the command-line options in order to know what is available.

### 9.4.2. The GIMP

The GIMP (which expands to either GNU Image Manipulation Program or General Image Manipulation Program and is often written with the definite article) specializes in image-manipulation like the ImageMagick package described in the last section. But while ImageMagick's strength is batch processing, GIMP does everything via its GUI. The GIMP is often cited as one of the most impressive and successful products for Linux (and other Unix versions). People who use image-manipulation programs professionally have said that while GIMP can feel a little bit awkward to use, it is functionally comparable to its commercial competitor from the Windows and Macintosh world, Adobe Photoshop. Some people even have called it "the free alternative to Photoshop."

GIMP draws its power from an amazing number of plug-ins that are available for it. Thus, in order to be able to use a newly written image manipulation filter or file format import filter, you only need to install the plug-in, restart GIMP, and you are ready to go.

In addition, GIMP uses a clever tiling mechanism that allows you to load arbitrarily large images into it; GIMP will keep in memory only the part that is currently being used or visible. And if these features are not enough, GIMP also has its own scripting language called script-fu.

GIMP comes with most Linux distributions on the market today and can be run simply by entering the command `gimp` in a shell after starting X. If your distribution does not have GIMP, you can get the source code from www.gimp.org. If you plan to do more work with GIMP, you should check this web site anyway, because it also contains the documentation and sample images that show off what you can do with GIMP. A screenshot showing the toolbar, dialog boxes, and a work area is shown in Figure 9-5.

#### Figure 9-5. The Gimp

### 9.4.3. POVRAY

While ImageMagick and GIMP are mainly for manipulating existing images, POVRAY (the first three letters stand for "Persistence Of Vision") is a program for automatically creating new images. It is a so-called ray-tracer; a program that computes a scene from some information about which objects should be in the scene, what their physical and optical properties are, where the light is coming from, and where the spectator is standing. You describe this to POVRAY in a programming language of its own.

This brief description should already indicate that using POVRAY is not something you learn in one afternoon. If you are interested in this program, you can download it from http://www.povray.org, which is also the first source for all kind of POVRAY resources like sample files, documentation, and so on. Since we could not give you even a crash course into using POVRAY here, we'll refrain from attempting this futile task.

Copyright © 2001 O'Reilly & Associates. All rights reserved.